package com.hnweaver.Util;

import weaver.general.BaseBean;
import weaver.general.GCONST;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public  class LogInfoUtil {
	/**
	 * 日志 升级方法
	 * @param path
	 * @param str
	 * @return
	 */
	private  String folderName;//文件名称
    static SimpleDateFormat newDf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd");
    private PrintWriter logPrint;
    private String logFile = "";
    BaseBean log=null;

	public LogInfoUtil(String folderName){
		this.folderName = folderName;
	}
	
    private String getLogFile(){
    	//获取当前系统路径
    	String sysPath=GCONST.getRootPath();
    	if(sysPath==null){
    		sysPath=System.getProperty("user.dir");
    	}
    	if(!"".equals(folderName)){
    		sysPath += "/hnweaver/log/"+folderName+"/"+folderName+"_"+df.format(new Date())+".log";
    	}else {
    		sysPath += "/hnweaver/log/log/log_"+df.format(new Date())+".log";
		}
        return sysPath;
    }

	public LogInfoUtil(){
		
	}

	private synchronized  void  logWriters(Object str,Throwable e1){
    	log=new BaseBean();
		logFile = getLogFile();
        try{
            logPrint = new PrintWriter(new FileWriter(logFile, true), true);
            logPrint.println(newDf.format(new Date())+"--"+getMyGrandpaStackTrace()+"-->>>>" + str);
            if(e1!=null){
            	log.writeLog(str.toString(),e1);
            	e1.printStackTrace(logPrint);
            }
            logPrint.flush();
            logPrint.close();
        }catch(IOException e2){
            try{
            	 File file=new File(logFile);
            	 if(!file.getParentFile().exists()) {  
                     //如果目标文件所在的目录不存在，则创建父目录  
                     if(file.getParentFile().mkdirs()){
                    	 file.createNewFile();
                     }
                 }  
                 logPrint = new PrintWriter(new FileWriter(logFile, true), true);
                 logPrint.println(newDf.format(new Date())+"--"+getMyGrandpaStackTrace()+"-->>>>" + str);
                 if(e1!=null){
                 	log.writeLog(str.toString(),e1);
                 	e1.printStackTrace(logPrint);
                 }
            }catch(IOException ex){
            	log.writeLog("Log记录出错了",ex);
            }
        }finally{
        	if(logPrint!=null){
        		logPrint.flush();
        		logPrint.close();
        	}
        }
	}
	
    public void logWriter(Object msg) {
    	if(msg instanceof Exception){
    		logWriters("",(Throwable)msg);
    	}else{
    		logWriters(msg,null);	
    	}
    }
   
    public void logWriter(Object msg,Throwable e) {
    	if(msg instanceof Exception){
    		log.writeLog("非异常对象，不输出日记");
    	}else{
    		logWriters(msg,e);
    	}
    }

	/**
	 * 获取被调用方法
	 * @return
	 */
	private  String  getMyGrandpaStackTrace(){
		StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		StackTraceElement log = stackTrace[2];
		String tag = null;
		for (int i = 1; i < stackTrace.length; i++) {
			StackTraceElement e = stackTrace[i];
			if (!e.getClassName().equals(log.getClassName())) {
				tag = e.getClassName() + "." + e.getMethodName()+"["+e.getLineNumber()+"]";
				break;
			}
		}
		if (tag == null) {
			tag = log.getClassName() + "." + log.getMethodName();

		}
		return tag;
	}
}
